package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;

//import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import sun.security.x509.AuthorityKeyIdentifierExtension;
import sun.security.x509.KeyIdentifier;
import tr.gov.tubitak.bilgem.esya.certviewer.Bundle;
import tr.gov.tubitak.bilgem.esya.certviewer.EByteTools;

import java.io.IOException;
import java.util.Enumeration;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 12/4/11
 * Time: 4:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class EAuthorityKeyIdentifierExtension implements ECertExtension{
    AuthorityKeyIdentifierExtension akiExtension ;
    @Override
    public String getName() {
        return Bundle.getString("ECertField.AuthorityKeyIdentifier");
    }

    @Override
    public String getShortTitle() {
        String retShortTitle="";
        Enumeration elements = akiExtension.getElements();
        while (elements.hasMoreElements())
        {
            Object o = elements.nextElement();
            if(o instanceof String)
            {
                String elementName = (String) o;
                try {
                    Object elementObject = akiExtension.get(elementName);
                    if(elementObject == null)
                    {
                        continue;
                    }
                    if(elementObject instanceof KeyIdentifier)
                    {
                        KeyIdentifier keyId = (KeyIdentifier) elementObject;
                        retShortTitle += "KeyID = "+EByteTools.toHexString(keyId.getIdentifier()," ")+"\n";
                    }
                    //<TODO> Buraya diğer tipler içinde alanları ekle
                    else
                    {
                        retShortTitle = EByteTools.toHexString(akiExtension.getExtensionValue()," ");
                    }

                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }
        return retShortTitle;
    }

    @Override
    public String getLongTitle() {
       return getShortTitle();
    }

    @Override
    public boolean isCritical() {
        return akiExtension.isCritical();
    }

    @Override
    public void setAsnObject(Object asnObject)
    {
        if(asnObject instanceof AuthorityKeyIdentifierExtension)
            akiExtension = (AuthorityKeyIdentifierExtension)asnObject;
     }

    public void encode(byte[] data)
    {
        /*
        ByteOutputStream byteOutputStream = new ByteOutputStream(data.length);
        byteOutputStream.write(data);
      //  byteOutputStream.close();
        try {
            akiExtension = new AuthorityKeyIdentifierExtension(false,null);
            akiExtension.encode(byteOutputStream);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } */
    }
}
